package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.Admin;
import io.geekidea.springbootplus.system.entity.AdminSchool;
import io.geekidea.springbootplus.system.entity.School;
import io.geekidea.springbootplus.system.param.AdminSchoolPageParam;
import io.geekidea.springbootplus.system.service.AdminSchoolService;
import io.geekidea.springbootplus.system.service.AdminService;
import io.geekidea.springbootplus.system.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-10-10
 */
@Slf4j
@RestController
@RequestMapping("/adminSchool")
@Module("system")
@Api(value = "API", tags = {""})
public class AdminSchoolControllerExtra extends BaseController {

    @Autowired
    private AdminSchoolService adminSchoolService;

    @Autowired
    private AdminService adminService;


    @Autowired
    private SchoolService schoolService;

//
//    /**
//     * 添加
//     */
//    @PostMapping("/open")
//    @OperationLog(name = "开通", type = OperationLogType.ADD)
//    @ApiOperation(value = "开通", response = ApiResult.class)
//    public ApiResult<Boolean> open(@Validated(Add.class) @RequestBody AdminSchool adminSchool) throws Exception {
//        adminSchool.setDeleted(0);
//        adminSchoolService.saveOrUpdate(adminSchool);
//
//        Admin admin = adminService.getById(adminSchool.getAdminID());
//        admin.setDeleted(0);
//        adminService.saveOrUpdate(admin);
//
//        return ApiResult.result(true);
//    }
//    @PostMapping("/refuse")
//    @OperationLog(name = "拒绝", type = OperationLogType.ADD)
//    @ApiOperation(value = "拒绝", response = ApiResult.class)
//    public ApiResult<Boolean> refuse(@Validated(Add.class) @RequestBody AdminSchool adminSchool) throws Exception{
//
//        adminSchoolService.removeById(adminSchool.getId());
//        return ApiResult.result(true);
//    }

    /**
     * 添加
     */
    @PostMapping("/pass")
    @OperationLog(name = "审批", type = OperationLogType.ADD)
    @ApiOperation(value = "审批", response = ApiResult.class)
    public ApiResult<Boolean> apply(@Validated(Add.class) @RequestBody AdminSchool adminSchool) throws Exception {
        adminSchool.setDeleted(0);
        adminSchoolService.saveOrUpdate(adminSchool);

        Admin admin = adminService.getById(adminSchool.getAdminID());
        admin.setDeleted(0);
        adminService.saveOrUpdate(admin);

        return ApiResult.result(true);
    }
    @PostMapping("/refuse")
    @OperationLog(name = "拒绝", type = OperationLogType.ADD)
    @ApiOperation(value = "拒绝", response = ApiResult.class)
    public ApiResult<Boolean> refuse(@Validated(Add.class) @RequestBody AdminSchool adminSchool) throws Exception{

        adminSchoolService.removeById(adminSchool.getId());
        return ApiResult.result(true);
    }


    /**
     * 全部
     */
    @PostMapping("/all")
    @OperationLog(name = "全部", type = OperationLogType.ADD)
    @ApiOperation(value = "全部", response = ApiResult.class)
    public ApiResult<Paging<AdminSchool>> all(@RequestBody AdminSchoolPageParam adminSchoolPageParam) throws Exception {

        Page<AdminSchool> page = new PageInfo<>(adminSchoolPageParam, OrderItem.desc("CreateTime"));
        LambdaQueryWrapper<AdminSchool> wrapper = new LambdaQueryWrapper<AdminSchool>();

        log.error(JSON.toJSONString(adminSchoolPageParam));
        IPage<AdminSchool> iPage = adminSchoolService.page(page, wrapper);

        Paging<AdminSchool> d = new Paging<AdminSchool>(iPage);

        List<AdminSchool> list = d.getRecords();

        if(list!=null){
            list.forEach(item->{
                School school = schoolService.getById(item.getSchoolID());
                item.setSchoolName(school.getSchoolName());

                Admin admin = adminService.getById(item.getAdminID());
                item.setAdminName(admin.getPersonName());

                item.setPhone(admin.getPhone());
            });
        }

        return ApiResult.ok(d);
    }

    /**
     * 等待审批
     */
    @PostMapping("/waitingCheck")
    @OperationLog(name = "等待审批", type = OperationLogType.ADD)
    @ApiOperation(value = "等待审批", response = ApiResult.class)
    public ApiResult<Paging<AdminSchool>> waitingCheck(AdminSchoolPageParam adminSchoolPageParam) throws Exception {

        Page<AdminSchool> page = new PageInfo<>(adminSchoolPageParam, OrderItem.desc("CreateTime"));
        LambdaQueryWrapper<AdminSchool> wrapper = new LambdaQueryWrapper<AdminSchool>().eq(AdminSchool::getDeleted,1);

        IPage<AdminSchool> iPage = adminSchoolService.page(page, wrapper);

        Paging<AdminSchool> d = new Paging<AdminSchool>(iPage);

        List<AdminSchool> list = d.getRecords();

        if(list!=null){
            list.forEach(item->{
                School school = schoolService.getById(item.getSchoolID());
                item.setSchoolName(school.getSchoolName());

                Admin admin = adminService.getById(item.getAdminID());
                item.setAdminName(admin.getPersonName());

                item.setPhone(admin.getPhone());
            });
        }

        return ApiResult.ok(d);
    }

}

